
import Api from '@/database'
import { defineWrappedResponseHandler } from '../utils/handler'

const handler = defineEventHandler(async (e) => {
  const result: any[] = []
  const tables = await Api.base.showTables()
  for (let i = 0; i < tables.length; i++) {
    const total = await Api.aggregate.count(tables[i])
    const item: any = {
      used: 0,
      label: tables[i],
      value: total[0]['count(*)']
    }
    const state = await Api.column.exists(tables[i], 'used')
    if (state) {
      const rate = await Api.used.get(tables[i])
      item.used = rate
    }
    result.push(item)
  }
  return result
})

export default defineWrappedResponseHandler(handler)